package servergui;

import java.util.Vector;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.LookAndFeel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Backkom
 */
public class RowHeader extends JTable implements ListSelectionListener {

    protected JTable mainTable;
    protected TableValues datas;
    public RowCountHeader values;

    public RowHeader(JTable table, TableValues tbValues) {
        super();
        mainTable = table;
        datas = tbValues;
        values = new RowCountHeader();
        setModel(values);
        setOpaque(false);       
        setDoubleBuffered(true);
        setPreferredScrollableViewportSize(this.getMinimumSize());       
        setCellSelectionEnabled(true);
        setShowGrid(true);
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        getSelectionModel().addListSelectionListener(this);
        JComponent renderer = (JComponent) getDefaultRenderer(Object.class);
        LookAndFeel.installColorsAndFont(renderer, "TableHeader.background", "TableHeader.foreground", "TableHeader.font");
        LookAndFeel.installBorder(this, "TableHeader.cellBorder");
    }

    @Override
    public int getRowHeight(int row) {
        return mainTable.getRowHeight();
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        if (e.getSource() == this.getSelectionModel()) {
            int SelectIndex = this.getSelectedRow();
            if (mainTable.getModel().getRowCount() == 0) {
                datas.addLine();
                values.fireTableDataChanged();
                mainTable.setModel(datas);
                mainTable.paintAll(mainTable.getGraphics());
            }
            if (SelectIndex == mainTable.getModel().getRowCount() - 1) {
                datas.addLine();
                values.fireTableDataChanged();
                mainTable.setModel(datas);
                mainTable.paintAll(mainTable.getGraphics());
            }
            this.getSelectionModel().clearSelection();
            
        }
    }

    class RowCountHeader extends AbstractTableModel {

        @Override
        public int getRowCount() {
            if (mainTable.getModel().getRowCount() == 0) {
                return 1;
            }
            return mainTable.getModel().getRowCount();
        }

        @Override
        public int getColumnCount() {
            return 1;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            if (rowIndex == (getRowCount() - 1)) {
                return "*";
            }
            return new Integer(rowIndex + 1);
        }
    }
}
